﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IntelligentEscalator.BUS;
using IntelligentEscalator.ElevatorWebSocket;
using IntelligentEscalator.DAL;
using IntelligentEscalator.Models;

namespace IntelligentEscalator
{
    public class OperationJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                MqttDataBUS mqttDataBUS = new MqttDataBUS();
                MqttDataDAL mqttDataDAL = new MqttDataDAL();
                int runtime = (int)mqttDataDAL.QueryKoneRunTime() / 3600;
                mqttDataBUS.MqttDataProcess("04", "KONE", "TodayRuntime", runtime);
                int fault = mqttDataDAL.QueryTotalFault();
                mqttDataBUS.MqttDataProcess("04", "faultTotal", "FaultStatistics", fault);
                int todayfault = mqttDataDAL.QueryTodayFault();
                mqttDataBUS.MqttDataProcess("04", "faultToday", "FaultStatistics", todayfault);
                bool result_7 = PushKoneHomeMessage("RefreshHomePage").Result;
            }
            catch
            {

            }
        }

        public static async Task<bool> PushKoneHomeMessage(string message)
        {
            try
            {
                if (ElevatorWebSocketMiddleware.webSocketManage != null)
                {
                    return await ElevatorWebSocketMiddleware.webSocketManage.PushKoneHomeMessage(message);
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public static void UpdateElevatorNum()
        {
            KoneDeviceStatus koneDeviceStatus = ElevatorStateBUS.KoneDeviceStatus;
            MqttDataDAL mqttDataDAL = new MqttDataDAL();
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    string type = string.Empty;
                    int datanum = 0;
                    mqttdata mqttdata = new mqttdata();

                    if (i == 0)
                    {
                        type = "normalTotal";
                        datanum = koneDeviceStatus.normalTotal;
                    }
                    else if (i == 1)
                    {
                        type = "faultTotal";
                        datanum = koneDeviceStatus.faultTotal;
                    }
                    else if (i == 2)
                    {
                        type = "maintenanceTotal";
                        datanum = koneDeviceStatus.maintenanceTotal;
                    }
                    else if (i == 3)
                    {
                        type = "offlineTotal";
                        datanum = koneDeviceStatus.offlineTotal;
                    }
                    else if (i == 4)
                    {
                        type = "deviceTotal";
                        datanum = koneDeviceStatus.deviceTotal;
                    }
                    mqttdata.dataNum = datanum;
                    mqttdata.manufacturer = "KONE";
                    mqttdata.manufacturerId = "04";
                    mqttdata.topicType = "DeviceStatusTotal";
                    mqttdata.dataType = type;
                    bool result = mqttDataDAL.CheckData(mqttdata.manufacturer, mqttdata.manufacturerId, type, mqttdata.topicType);

                    if (result)
                    {
                        mqttDataDAL.UpdateMqttData(mqttdata);
                    }
                    else
                    {
                        mqttDataDAL.AddMqttData(mqttdata);
                    }
                }
                catch
                {
                    continue;
                }
            }
        }
    }
}
